package com.wffz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wffz.mapper.WithDrawRecordMapper;
import com.wffz.po.ClientUser;
import com.wffz.po.MoneyLog;
import com.wffz.po.WithdrawRecord;
import com.wffz.service.WithDrawRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author sunlei
 * @className WithDrawRecordServiceImpl
 * @Description
 * @date 2023-03-04 6:52
 **/
@Service
public class WithDrawRecordServiceImpl implements WithDrawRecordService {

    @Resource
    private WithDrawRecordMapper withDrawRecordMapper;

    @Override
    public boolean saveWithDrawRecord(WithdrawRecord withdrawRecord) {
        return withDrawRecordMapper.insert(withdrawRecord) > 0;
    }

    @Override
    public PageInfo<WithdrawRecord> getWithDrawRecord(Integer pageNum, Integer pageSize, String loginName) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<WithdrawRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name", loginName);
        wrapper.orderByDesc("create_time");
        List<WithdrawRecord> moneyLogs = withDrawRecordMapper.selectList(wrapper);
        PageInfo<WithdrawRecord> pageInfo = new PageInfo<>(moneyLogs);
        return pageInfo;
    }

    @Override
    public Float withDrawMoney(ClientUser clientUser) {
        float result = 0;
        QueryWrapper<WithdrawRecord> queryWrapper = new QueryWrapper<WithdrawRecord>();
        queryWrapper.select("sum(amount) as amount");
        queryWrapper.eq("user_id", clientUser.getId());
        WithdrawRecord ord = withDrawRecordMapper.selectOne(queryWrapper);
        //注意，空指针问题
        if (ord == null) {
            result = Float.valueOf(0);
        } else {
            result = ord.getAmount();
        }
        return result;
    }

    @Override
    public int withDrawCount(ClientUser clientUser) {
        QueryWrapper<WithdrawRecord> queryWrapper = new QueryWrapper<WithdrawRecord>();
        queryWrapper.eq("user_id", clientUser.getId());
        return withDrawRecordMapper.selectCount(queryWrapper);
    }
}
